Imports System.IO
Imports System.Xml
Imports System.Text

Module FileHandlingLibrary
	''' <summary>
	''' Read the actual bytes of a file.
	''' </summary>
	''' <param name="filepath"></param>
	''' <returns></returns>
	''' <remarks>For certain text files this will include the Byte Order Marker (BOM) at the beginning of the file so may produce unexpected results.
	''' See http://andrewmatthewthompson.blogspot.co.uk/2011/02/byte-order-mark-found-using-net.html</remarks>
	Public Function GetBinaryFileBytes(filepath As String) As Byte()
		Using fs As New FileStream(filepath, FileMode.Open, FileAccess.Read), br As New BinaryReader(fs)
			Dim data(CInt(fs.Length)) As Byte
			data = br.ReadBytes(CInt(fs.Length))
			Return data
		End Using
	End Function
	''' <summary>
	''' Read the bytes of a text file without the Byte Order Marker(BOM).
	''' </summary>
	''' <param name="filepath"></param>
	''' <returns></returns>
	''' <remarks>See http://andrewmatthewthompson.blogspot.co.uk/2011/02/byte-order-mark-found-using-net.html</remarks>
	Public Function GetTextFileBytes(filepath As String) As Byte()
		Using fs As New FileStream(filepath, FileMode.Open, FileAccess.Read), sr As New StreamReader(fs, True)
			Dim data As String = sr.ReadToEnd
			Return Encoding.UTF8.GetBytes(data)
		End Using
	End Function

	' Return the file's contents in a string.
	Public Function GetFileContents(ByVal FileName As String) As String
		Using sr As New StreamReader(FileName)
			Dim text As String = sr.ReadToEnd()
			sr.Close()
			Return text
		End Using
	End Function

	' Write a file from a string.
	Public Sub SetFileContents(ByVal FileName As String, ByVal FileContents As String)
		Using sw As New StreamWriter(FileName)
			sw.Write(FileContents)
			sw.Close()
		End Using
	End Sub

	' Return the location of the Data subdirectory
	' without a trailing \.
	Public Function DataSubdirectory() As String
		Dim startup_path As String
		Dim data_path As String
		Dim pos As Integer

		' Remove the trailing \ if any. Normally 
		' StartupPath returns "C:\" but "C:\Test".
		startup_path = Application.StartupPath
		If startup_path.EndsWith("\") Then startup_path = startup_path.Remove(startup_path.Length - 1, 1)

		' Look in the startup directory.
		data_path = startup_path & "\Data"
		If Directory.Exists(data_path) Then
			' Return the startup directory's Data subdirectory.
			Return data_path
		End If

		' There is no Data directory here.
		' Look up one level.
		pos = startup_path.LastIndexOf("\")
		If pos = 0 Then
			' There is no parent directory (e.g. we're at C:).
			' Return the startup path.
			Return startup_path
		End If

		' Look for the Data directory in the parent directory.
		data_path = startup_path.Substring(0, pos) & "\Data"
		If Directory.Exists(data_path) Then
			' The parent has a Data directory.
			' Return it.
			Return data_path
		End If

		' Our parent directory doesn't have a
		' Data subdirectory. Return the startup path.
		Return startup_path
	End Function

	' Return the file's name without its path.
	Public Function FileTitle(ByVal FileName As String) As String
		Dim pos As Integer

		pos = InStrRev(FileName, "\")
		If pos = 0 Then pos = InStrRev(FileName, ":")
		If pos = 0 Then
			Return FileName
		Else
			Return FileName.Substring(pos)
		End If
	End Function
End Module
